using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;

public class TextureChannelTool : EditorWindow {



	public enum OPTIONS
	{
		R = 0,
		G = 1,
		B = 2,
		A = 3,


        R_G2L = 4,
        G_G2L = 5,
        B_G2L = 6,
        A_G2L = 7,


        R_L2G = 8,
        G_L2G = 9,
        B_L2G = 10,
        A_L2G = 11,

        R_INV = 14,
        G_INV = 15,
        B_INV = 16,
        A_INV = 17,
        Black = 18,
        Write = 19,
        Gray = 20,
    }

	[MenuItem("TA/辅助/图层通道合并")]
	static void Init()
	{
		EditorWindow.GetWindow<TextureChannelTool>("图层通道合并工具").Show ();
	}
	Texture2D[] editorArray = new Texture2D[4];
	OPTIONS [] types = new OPTIONS[]{OPTIONS.R,OPTIONS.R,OPTIONS.R,OPTIONS.R};
    int csvIndex = 1;
    int subIndex = 0;

    void CombineMesh(string savePath)
	{
		Texture2D black = new Texture2D (1, 1, TextureFormat.RGBA32, false);
		black.SetPixel (0, 0, Color.black);
		black.Apply ();
		int width = 0;
		int height = 0;
		for (int i = 0; i < editorArray.Length; i++) {
			if (null == editorArray [i]) {
				editorArray [i] = black;
				continue;
			}
			width = Mathf.Max (width, editorArray [i].width);
			height = Mathf.Max (height, editorArray [i].height);
		}
		if (width == 0)
			return;
		RenderTexture [] temp = new RenderTexture[ editorArray.Length];
		Texture2D [] temp2 = new Texture2D[ editorArray.Length];
 
		for (int i = 0; i < editorArray.Length; i++) {
			temp[i]  = RenderTexture.GetTemporary (width, height);

			if (null != editorArray [i]) {
				Graphics.Blit (editorArray [i], temp [i]);
			}
		 

			temp2 [i] = new Texture2D (width, height,TextureFormat.RGBA32,false);
		 
			RenderTexture.active = 	temp[i] ;
			temp2 [i] .ReadPixels(new Rect(0, 0, width, height), 0, 0);
			temp2 [i].Apply ();
			RenderTexture.ReleaseTemporary (temp[i] );
		}

		Texture2D final = new Texture2D (width, height, TextureFormat.RGBA32,false);
		float [] _cols = new float[4];
		for(int i = 0 ; i < width ;i++)
		{
			for (int j = 0; j < height; j++) {
				for(int k = 0 ; k < 4 ; k++)
				{
					switch (types [k]) {
					case OPTIONS.R:
						_cols[k] = temp2 [k].GetPixel (i, j).r;
						break;
					case OPTIONS.G:
						_cols[k] = temp2 [k].GetPixel (i, j).g;
						break;
					case OPTIONS.B:
						_cols[k] = temp2 [k].GetPixel (i, j).b;
						break;
                    case OPTIONS.A:
                            _cols[k] = temp2 [k].GetPixel (i, j).a;
						break;


                    case OPTIONS.R_G2L:
                        _cols[k] = Mathf.GammaToLinearSpace( temp2[k].GetPixel(i, j).r);
                        break;
                    case OPTIONS.G_G2L:
                        _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).g);
                        break;
                    case OPTIONS.B_G2L:
                        _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).b);
                        break;
                    case OPTIONS.A_G2L:
                        _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).a);
                        break;

                    case OPTIONS.R_L2G:
                        _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).r);
                        break;
                    case OPTIONS.G_L2G:
                        _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).g);
                        break;
                    case OPTIONS.B_L2G:
                        _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).b);
                        break;
                    case OPTIONS.A_L2G:
                        _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).a);
                        break;


                    case OPTIONS.R_INV:
                        _cols[k] = 1.0f - temp2[k].GetPixel(i, j).r;
                        break;
                    case OPTIONS.G_INV:
                        _cols[k] = 1.0f - temp2[k].GetPixel(i, j).g;
                        break;
                    case OPTIONS.B_INV:
                        _cols[k] = 1.0f - temp2[k].GetPixel(i, j).b;
                        break;
                    case OPTIONS.A_INV:
                        _cols[k] = 1.0f - temp2[k].GetPixel(i, j).a;
                        break;
                        case OPTIONS.Black:
                            {
                                _cols[k] = 0;
                            }
                            break;
                        case OPTIONS.Gray:
                            {
                                _cols[k] = 0.5f;
                            }
                            break;
                        case OPTIONS.Write:
                            {
                                _cols[k] = 1f;
                            }
                            break;
                    }
				}

				 
				final.SetPixel (i, j, new Color(_cols[0] ,  _cols[1] ,  _cols[2] ,_cols[3] ));
			}
		}
		final.Apply ();
		for (int i = 0; i < editorArray.Length; i++) {
			GameObject.DestroyImmediate (temp2 [i]);
		}
		byte[] date = TgaUtil.Texture2DEx.EncodeToTGA (final, true);
		//byte [] date =  final.EncodeToPNG ();
		System.IO.File.WriteAllBytes (savePath, date);
		GameObject.DestroyImmediate (final);
		GameObject.DestroyImmediate (black);

	}
    

     
    void CombineMesh2(string savePath)
    {
        Texture2D black = new Texture2D(1, 1, TextureFormat.RGBA32, false);
        black.SetPixel(0, 0, Color.black);
        black.Apply();
        int width = 0;
        int height = 0;
        for (int i = 0; i < editorArray.Length; i++)
        {
            if (null == editorArray[i])
            {
                editorArray[i] = black;
                continue;
            }
            width = Mathf.Max(width, editorArray[i].width);
            height = Mathf.Max(height, editorArray[i].height);
        }
        if (width == 0)
            return;
        RenderTexture[] temp = new RenderTexture[editorArray.Length];
        Texture2D[] temp2 = new Texture2D[editorArray.Length];

        for (int i = 0; i < editorArray.Length; i++)
        {
            temp[i] = RenderTexture.GetTemporary(width, height);

            if (null != editorArray[i])
            {
                Graphics.Blit(editorArray[i], temp[i]);
            }


            temp2[i] = new Texture2D(width, height, TextureFormat.RGBA32, false);

            RenderTexture.active = temp[i];
            temp2[i].ReadPixels(new Rect(0, 0, width, height), 0, 0);
            temp2[i].Apply();
            RenderTexture.ReleaseTemporary(temp[i]);
        }

        Texture2D final = new Texture2D(width, height, TextureFormat.RGBA32, false);
        float[] _cols = new float[4];
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    switch (types[k])
                    {
                        case OPTIONS.R:
                            _cols[k] = temp2[k].GetPixel(i, j).r;
                            break;
                        case OPTIONS.G:
                            _cols[k] = temp2[k].GetPixel(i, j).g;
                            break;
                        case OPTIONS.B:
                            _cols[k] = temp2[k].GetPixel(i, j).b;
                            break;
                        case OPTIONS.A:
                            _cols[k] = temp2[k].GetPixel(i, j).a;
                            break;


                        case OPTIONS.R_G2L:
                            _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).r);
                            break;
                        case OPTIONS.G_G2L:
                            _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).g);
                            break;
                        case OPTIONS.B_G2L:
                            _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).b);
                            break;
                        case OPTIONS.A_G2L:
                            _cols[k] = Mathf.GammaToLinearSpace(temp2[k].GetPixel(i, j).a);
                            break;

                        case OPTIONS.R_L2G:
                            _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).r);
                            break;
                        case OPTIONS.G_L2G:
                            _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).g);
                            break;
                        case OPTIONS.B_L2G:
                            _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).b);
                            break;
                        case OPTIONS.A_L2G:
                            _cols[k] = Mathf.LinearToGammaSpace(temp2[k].GetPixel(i, j).a);
                            break;


                        case OPTIONS.R_INV:
                            _cols[k] = 1.0f - temp2[k].GetPixel(i, j).r;
                            break;
                        case OPTIONS.G_INV:
                            _cols[k] = 1.0f - temp2[k].GetPixel(i, j).g;
                            break;
                        case OPTIONS.B_INV:
                            _cols[k] = 1.0f - temp2[k].GetPixel(i, j).b;
                            break;
                        case OPTIONS.A_INV:
                            _cols[k] = 1.0f - temp2[k].GetPixel(i, j).a;
                            break;
                        case OPTIONS.Black:
                            {
                                _cols[k] = 0;
                            }
                            break;
                        case OPTIONS.Gray:
                            {
                                _cols[k] = 0.5f;
                            }
                            break;
                        case OPTIONS.Write:
                            {
                                _cols[k] = 1f;
                            }
                            break;

                        default:
                            _cols[k] = temp2[k].GetPixel(i, j).a;
                            break;
                    }
                }

                final.SetPixel(i, j, new Color(_cols[0], _cols[1], _cols[2], _cols[3]));
            }
        }
        final.Apply();
        for (int i = 0; i < editorArray.Length; i++)
        {
            GameObject.DestroyImmediate(temp2[i]);
        }
        byte[] date = final.EncodeToPNG();
        //byte [] date =  final.EncodeToPNG ();
        System.IO.File.WriteAllBytes(savePath, date);
        GameObject.DestroyImmediate(final);
        GameObject.DestroyImmediate(black);

    }
    int toolBar = 0;
    bool isTGA = false;


    bool AddUVToObject(string ObjPath, string CsvPath, int index0, int StartIndex)
    {
        char[] spes = new char[1] { '/' };
        char[] spesSp = new char[1] { ' ' };
        try
        {
            if (!System.IO.File.Exists(ObjPath))
            {
                Debug.Log("路径1[" + ObjPath + "]不存在");
                return false;
            }
            if (!System.IO.File.Exists(CsvPath))
            {
                Debug.Log("路径2[" + CsvPath + "]不存在");
                return false;
            }
            string[] lines = System.IO.File.ReadAllLines(ObjPath);

            string[] csvs = System.IO.File.ReadAllLines(CsvPath);

            List<string> finals = new List<string>();
            int index = 0;
            //加入v
            for (; index < lines.Length; index++)
            {
                string line = lines[index];
                if (line.StartsWith("v "))
                {
                    finals.Add(line);
                }
                else
                {
                    break;
                }
            }
            //跳过vt
            for (; index < lines.Length; index++)
            {
                string line = lines[index];
                if (line.StartsWith("vt "))
                {

                }
                else
                {
                    break;
                }
            }
            int count = 0;
            for (int i = 1; i < csvs.Length; i++)
            {
                string l1 = csvs[i];
                string[] arrays1 = l1.Split(',');
                if (arrays1[0] == "0")
                    count++;
                else
                    break;
            }

            //int count

            //加入新vt
            for (int i = 1; i < csvs.Length; i += count)
            {
                string l1 = csvs[i + StartIndex];
                string l2 = csvs[i + StartIndex + 1];
                string[] arrays1 = l1.Split(',');
                string[] arrays2 = l2.Split(',');
                string ps1 = arrays1[index0];
                string ps2 = arrays2[index0];

                double v = double.Parse(ps2);
                while (v > 1f)
                    v -= 1f;
                while (v < 0)
                    v += 1f;
                v = 1 - v;
                finals.Add("vt " + ps1 + " " + v);
            }
            //加入vn
            for (; index < lines.Length; index++)
            {
                string line = lines[index];
                if (line.StartsWith("vn "))
                {
                    finals.Add(line);
                }
                else
                {
                    break;
                }
            }
            //加入末尾
            for (; index < lines.Length; index++)
            {
                string line = lines[index];

                if (line.StartsWith("f "))
                {
                    string _l = "f ";
                    string[] _params = line.Split(spesSp, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 1; j < _params.Length; j++)
                    {
                        string _p = _params[j];
                        string[] _ps = _p.Split(spes, StringSplitOptions.RemoveEmptyEntries);
                        if (_ps.Length == 3)
                        {
                            _l += _p + " ";
                        }
                        else
                        {
                            _l += _ps[0] + "/" + _ps[0] + "/" + _ps[0] + " ";
                        }
                    }
                    finals.Add(_l);
                }
                else
                {
                    finals.Add(line);
                }

            }
            //System.IO.File.WriteAllLines("D://Test.txt", finals);
            System.IO.File.WriteAllLines(ObjPath, finals);
            Debug.Log("合并完成");
            return true;
        }
        catch (Exception e)
        {
            Debug.Log("格式解析有错误");
        }
        return false;

    }
    void OnGUI()
	{
		
		GUILayout.Label ("R通道");
		editorArray [0] = (Texture2D)EditorGUILayout.ObjectField (editorArray [0], typeof(Texture2D));
		types[0] = (OPTIONS)EditorGUILayout.EnumPopup("来自通道:", types[0] );
		GUILayout.Label ("G通道");
		editorArray [1] = (Texture2D)EditorGUILayout.ObjectField (editorArray [1], typeof(Texture2D));
		types[1] = (OPTIONS)EditorGUILayout.EnumPopup("来自通道:", types[1] );
		GUILayout.Label ("B通道");
		editorArray [2] = (Texture2D)EditorGUILayout.ObjectField (editorArray [2], typeof(Texture2D));
		types[2] = (OPTIONS)EditorGUILayout.EnumPopup("来自通道:", types[2] );
		GUILayout.Label ("A通道");
		editorArray [3] = (Texture2D)EditorGUILayout.ObjectField (editorArray [3], typeof(Texture2D));
		types[3] = (OPTIONS)EditorGUILayout.EnumPopup("来自通道:", types[3] );

        isTGA = EditorGUILayout.Toggle("tga", isTGA);
        if (GUILayout.Button ("合成")) {
            Save();
			
		}
        if (GUILayout.Button("合成到第一张图"))
        {
            Save(false);
        }
        if (GUILayout.Button("GL_DX法线互转 "))
        {
            editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0];
            types[0] = OPTIONS.R;
            types[1] = OPTIONS.G_INV;
            types[2] = OPTIONS.B;
            types[3] = OPTIONS.A;
        }
        if (GUILayout.Button("一梦遮罩图转Unity "))
        {
            editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0];
            types[0] = OPTIONS.R;
            types[1] = OPTIONS.Write;
            types[2] = OPTIONS.G;
            types[3] = OPTIONS.B;

        }
        if (GUILayout.Button("第一张图交换ga通道"))
        {
            SaveGA( );

        }
        // csvIndex = EditorGUILayout.IntField("csvIndex", csvIndex);
        if (GUILayout.Button("All"))
        {
            editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0];
        }
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("NormalBA"))
        {
            editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0];
            types[0] = OPTIONS.B;
            types[1] = OPTIONS.A;
            types[2] = OPTIONS.Black;
            types[3] = OPTIONS.Write;
            Save();
        }
        if (GUILayout.Button("NormalRG"))
        {
            editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0];
            types[0] = OPTIONS.R;
            types[1] = OPTIONS.G;
            types[2] = OPTIONS.Black;
            types[3] = OPTIONS.Write;
            Save();
        }
        if (GUILayout.Button("诛仙Mask"))
        {
            editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0];
            types[0] = OPTIONS.R;
            types[1] = OPTIONS.Black;
            types[2] = OPTIONS.Black;
            types[3] = OPTIONS.G;
            Save();
        }

        


        GUILayout.EndHorizontal();
        if (GUILayout.Button("诛仙一键"))
        {
            string path = EditorUtility.SaveFolderPanel("", "", "");
            if (path.Length > 0)
            {
                path +=  "/";
                string rpath = "Assets" + path.Substring(Application.dataPath.Length);
                DirectoryInfo root = new DirectoryInfo(path);
                FileInfo[] files = root.GetFiles();
                for (int i = 0; i < files.Length; i++)
                {
                    var f = files[i];
                    if (f.Extension.ToLower() == ".obj")
                    {
                        string _path = rpath + f.Name;
                        string csv_path = _path.Substring(0, _path.Length - 3)+"csv";

                        if (File.Exists(csv_path))
                        {
                            AddUVToObject(_path, csv_path, csvIndex,subIndex);
                            AssetDatabase.ImportAsset(_path);
                            try
                            {
                                File.Delete(csv_path);
                            }
                            catch (System.Exception e)
                            {
                                Debug.LogError(e.ToString());
                            }
                        }

                    }
                    else if (f.Extension.ToLower() == ".dds")
                    {
                        string _path = rpath + f.Name;

                        Texture2D t2d = AssetDatabase.LoadAssetAtPath<Texture2D>(_path);
                        editorArray[3] = editorArray[2] = editorArray[1] = editorArray[0] = t2d;
                        types[0] = OPTIONS.B;
                        types[1] = OPTIONS.A;
                        types[2] = OPTIONS.Black;
                        types[3] = OPTIONS.Write;

                        isTGA = true;
                        string b_path = _path.Substring(0, _path.Length - 4);
                        string normal_path = b_path + "_N.tga";
                        string mask_path = b_path + "_M.tga";
                        CombineMesh(normal_path);
                        AssetDatabase.ImportAsset(normal_path);


                        types[0] = OPTIONS.R;
                        types[1] = OPTIONS.Black;
                        types[2] = OPTIONS.Black;
                        types[3] = OPTIONS.G;
                        CombineMesh(mask_path);
                        AssetDatabase.ImportAsset(mask_path);
                        GameObject.DestroyImmediate(t2d, true);
                        try
                        {
                            File.Delete(_path);
                        }
                        catch (System.Exception e)
                        { 
                        }
                        
                    }
                }
                AssetDatabase.Refresh();
            }
            
           
        }
        if (GUILayout.Button("一键解诛仙到Lit"))
        {
            SaveLit();

        }

    }
    private void SaveLit( )
    {

        bool found = false;
        if (editorArray[0] != null)
            found = true;
        if (!found)
        {
            EditorUtility.DisplayDialog("提示", "没有图片呗选择", "ok");
            return;
        }
      
      
        {
            string path = AssetDatabase.GetAssetPath(editorArray[0]);
            if (path.Length != 0)
            {

                SaveNormal(path.Substring(0, path.Length-4)+"_N.png");
                if(null == editorArray[1])
                    SaveSNEN(path.Substring(0, path.Length - 4) + "_M.png");
                else
                    SaveS_MNEN(path.Substring(0, path.Length - 4) + "_M.png");


            }

        }
        AssetDatabase.Refresh();
    }
    void SaveSNEN(string savePath)
    {
     
        int width = 0;
        int height = 0;
        width = Mathf.Max(width, editorArray[0].width);
        height = Mathf.Max(height, editorArray[0].height);
        if (width == 0)
            return;
        RenderTexture temp = RenderTexture.GetTemporary(width, height);
        Graphics.Blit(editorArray[0], temp);
        Texture2D temp2 = new Texture2D(width, height, TextureFormat.RGBA32, false);

        RenderTexture.active = temp;
        temp2.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        temp2.Apply();
        RenderTexture.ReleaseTemporary(temp);

        Texture2D final = new Texture2D(width, height, TextureFormat.RGBA32, false);

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Color c = temp2.GetPixel(i, j);
 
                final.SetPixel(i, j, new Color(0f, 1f, c.b, c.r));
            }
        }
        final.Apply();
        GameObject.DestroyImmediate(temp2);
        byte[] date = final.EncodeToPNG();
        System.IO.File.WriteAllBytes(savePath, date);
        GameObject.DestroyImmediate(final);
 

    }

    void SaveS_MNEN(string savePath)
    {

        int width = 0;
        int height = 0;
        width = Mathf.Max(width, editorArray[0].width);
        height = Mathf.Max(height, editorArray[0].height);
        if (width == 0)
            return;
        RenderTexture temp = RenderTexture.GetTemporary(width, height);
        Graphics.Blit(editorArray[0], temp);
        Texture2D temp2 = new Texture2D(width, height, TextureFormat.RGBA32, false);

        RenderTexture.active = temp;
        temp2.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        temp2.Apply();
        

        Texture2D final = new Texture2D(width, height, TextureFormat.RGBA32, false);

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Color c = temp2.GetPixel(i, j);

                final.SetPixel(i, j, new Color(c.r, 1f, c.b, c.r));
            }
        }

  
        Graphics.Blit(editorArray[1], temp);
        RenderTexture.active = temp;
        temp2.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        temp2.Apply();


        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Color c0 = final.GetPixel(i, j);
                Color c = temp2.GetPixel(i, j);

                final.SetPixel(i, j, new Color(c0.r, c0.g, c0.b, c.a));
            }
        }

        final.Apply();

        RenderTexture.ReleaseTemporary(temp);
        GameObject.DestroyImmediate(temp2);
        byte[] date = final.EncodeToPNG();
        System.IO.File.WriteAllBytes(savePath, date);
        GameObject.DestroyImmediate(final);


    }
    void SaveNormal(string savePath)
    {
 
        int width = 0;
        int height = 0;
        width = Mathf.Max(width, editorArray[0].width);
        height = Mathf.Max(height, editorArray[0].height);
        if (width == 0)
            return;
        RenderTexture  temp = RenderTexture.GetTemporary(width, height);
        Graphics.Blit(editorArray[0], temp);
        Texture2D  temp2 = new Texture2D(width, height, TextureFormat.RGBA32, false);

        RenderTexture.active = temp ;
        temp2 .ReadPixels(new Rect(0, 0, width, height), 0, 0);
        temp2 .Apply();
        RenderTexture.ReleaseTemporary(temp );

        Texture2D final = new Texture2D(width, height, TextureFormat.RGBA32, false);
 
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Color c = temp2.GetPixel(i, j);
                float x = c.a * 2f - 1f;
                float y = c.g * 2f - 1f;
                float z = Mathf.Sqrt( 1f - (x * x + y * y) );
                final.SetPixel(i, j, new Color(c.a, c.g, z*0.5f+0.5f, 1.0f));
            }
        }
        final.Apply();
        GameObject.DestroyImmediate(temp2);
        byte[] date = final.EncodeToPNG();
        System.IO.File.WriteAllBytes(savePath, date);
        GameObject.DestroyImmediate(final);
 

    }

    private void SaveGA( )
    {
        
        string path = AssetDatabase.GetAssetPath(editorArray[0]);
        if (path.Length != 0)
        {
            editorArray[1] = editorArray[2] = editorArray[3] = editorArray[0];
            types[0] = OPTIONS.R;
            types[1] = OPTIONS.A;
            types[2] = OPTIONS.B;
            types[3] = OPTIONS.G;
            Save(false);
            AssetDatabase.ImportAsset(path);
        }
    }
    private void Save(bool select = true)
    {
        bool found = false;
        for (int i = 0; i < 4; i++)
        {
            if (editorArray[i] != null)
                found = true;
        }
        if (!found)
        {
            EditorUtility.DisplayDialog("提示", "没有图片呗选择", "ok");
            return;
        }
        if (isTGA)
        {
            
            string path = EditorUtility.SaveFilePanelInProject("Save Texture", "TextureName", "tga",
            "请输入保存文件名");
            if (path.Length != 0)
            {
                CombineMesh(path);
                AssetDatabase.ImportAsset(path);
            }
        }
        else
        {
            if (!select)
            {
                string path = AssetDatabase.GetAssetPath(editorArray[0]);
                if (path.Length != 0)
                {
                    CombineMesh2(path);
                    AssetDatabase.ImportAsset(path);
                }
            }
            else
            {
                string name = "TextureName";
                if (editorArray[0].name.Length > 0)
                {
                    name = editorArray[0].name;
                }
                string path = EditorUtility.SaveFilePanelInProject("Save Texture", name, "png",
            "请输入保存文件名");
                if (path.Length != 0)
                {
                    CombineMesh2(path);
                    AssetDatabase.ImportAsset(path);
                }
            }
            
        }
    }
}
